package com.qf.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.qf.hospital.dto.SysRoleDTO;
import com.qf.hospital.entity.SysRole;
import com.qf.hospital.enums.HospitalExceptionEnums;
import com.qf.hospital.service.SysRoleService;
import com.qf.hospital.vo.R;
import com.qf.hospital.vo.LayUIDTreeVO;
import com.qf.hospital.vo.SysRoleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ASUS
 * @version V1.0
 * @Project hospital
 * @Package com.qf.hospital.controller
 * @Date 2021/10/18 20:19
 */
@RestController
public class RoleController {

    private Logger log = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 根据条件查询权限
     * @param sysRoleDTO
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/role/loadAllRole")
    public R loadAllRole(SysRoleDTO sysRoleDTO,
                         @RequestParam(defaultValue = "1")Integer page,
                         @RequestParam(defaultValue = "10")Integer limit){
        PageInfo<SysRole> pageInfo = sysRoleService.loadAllRole(sysRoleDTO,page,limit);
        long count = pageInfo.getTotal();
        ArrayList<SysRoleVO> roleVOArrayList = new ArrayList<>();
        for (SysRole sysRole : pageInfo.getList()) {
            SysRoleVO sysRoleVO = new SysRoleVO();
            BeanUtils.copyProperties(sysRole,sysRoleVO);
            roleVOArrayList.add(sysRoleVO);
        }
        return R.ok(count,roleVOArrayList);
    }

    /**
     * 修改权限
     * @param sysRoleDTO
     * @return
     */
    @PostMapping("/role/updateRole")
    public R updateRole(SysRoleDTO sysRoleDTO){
        Integer roleId = sysRoleDTO.getRoleId();
        if(roleId == null || roleId <= 0){
            log.info("【修改权限】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        sysRoleService.updateRole(sysRoleDTO);
        return R.ok("修改权限成功");
    }

    /**
     * 添加权限
     * @param sysRoleDTO
     * @return
     */
    @PostMapping("/role/insertRole")
    public R insertRole(SysRoleDTO sysRoleDTO){
        sysRoleService.insertRole(sysRoleDTO);
        return R.ok("添加权限成功");
    }

    /**
     * 删除权限
     * @param roleid
     * @return
     */
    @PostMapping("/role/deleteRole")
    public R deleteRole(Integer roleid){
        if(roleid == null || roleid <= 0){
            log.info("【删除权限】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        sysRoleService.deleteRole(roleid);
        return R.ok("删除权限成功");
    }

    /**
     * 展示全部菜单信息&回显角色拥有的信息
     * @param roleid
     * @return
     */
    @PostMapping("/role/initRoleMenuTreeJson")
    public R initRoleMenuTreeJson(Integer roleid){
        if(roleid == null || roleid <= 0){
            log.info("【删除权限】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        List<LayUIDTreeVO> layUIDTreeVOList = sysRoleService.initRoleMenuTreeJson(roleid);
        return R.ok(layUIDTreeVOList);
    }

    /**
     * 分配菜单
     * @param roleid
     * @param ids
     * @return
     */
    @PostMapping("/role/saveRoleMenu")
    public R saveRoleMenu(Integer roleid,Integer[] ids){
        if(roleid == null || roleid <= 0){
            log.info("【分配菜单信息】 参数不合法！");
            return R.error(HospitalExceptionEnums.REQUEST_PARAMETER_ERROR,"");
        }
        sysRoleService.saveRoleMenu(roleid,ids);
        return R.ok("分配菜单成功");
    }


}
